package cn.wolfcode.crm.service.Impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.query.CustomerQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void save(Employee employee, Long[] ids) {
        employee.setPassword(new Md5Hash(employee.getPassword(), employee.getName()).toString());
        employeeMapper.insert(employee);
        if (ids != null && ids.length > 0) {
            for (Long roleId : ids) {
                employeeMapper.insertRelation(employee.getId(), roleId);
            }
        }
    }

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
        employeeMapper.deleteRelation(id);
    }

    @Override
    public void batchDelete(Long[] ids) {
        employeeMapper.batchDelete(ids);
    }

    @Override
    public Employee queryByusername(String username) {

        return employeeMapper.selectByusername(username);
    }

    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);
        employeeMapper.deleteRelation(employee.getId());
        if (ids != null && ids.length > 0) {
            for (Long roleId : ids) {
                employeeMapper.insertRelation(employee.getId(), roleId);
            }
        }
    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> list() {
        return employeeMapper.selectAll();
    }

    @Override
    public Employee login(String username, String password) {
        return employeeMapper.login(username, password);
    }

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(employeeMapper.selectForList(qo));
    }

    @Override
    public Workbook export() {
//        创建 excel 文件
        Workbook wb = new HSSFWorkbook();
        // 创建工作簿
        Sheet sheet = wb.createSheet();
        // 创建行，参数数行索引
        Row row = sheet.createRow(0);
        // 创建第一行单元格, 并设置单元格数据
        row.createCell(0).setCellValue("名称");
        row.createCell(1).setCellValue("邮箱");
        row.createCell(2).setCellValue("年龄");
        // 遍历设置每行数据
        List<Employee> employees = employeeMapper.selectAll();
        for (int i = 0; i < employees.size(); i++) {
            Employee employee = employees.get(i);
            // 每一个员工一行
            row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(employee.getName());
            row.createCell(1).setCellValue(employee.getEmail());
            row.createCell(2).setCellValue(employee.getAge());
        }
        return wb;
    }
    public void imports(InputStream inputStream) throws Exception {
        // 上传文件是不是在这个 inputStream
        Workbook wb = WorkbookFactory.create(inputStream);
        // workbook 有是有内容
        Sheet sheet = wb.getSheetAt(0);
        // 获取这个工作簿里面有多少行
        int lastRowIndex = sheet.getLastRowNum();
        for(int i = 1; i <= lastRowIndex; i++){
            Row row = sheet.getRow(i);

            Employee e = new Employee();
            String name = row.getCell(0).getStringCellValue();
            int age = new Double(row.getCell(1).getNumericCellValue()).intValue();
            String email = row.getCell(2).getStringCellValue();

            e.setName(name);
            e.setAge(age);
            e.setEmail(email);
            e.setPassword(new SimpleHash("md5", "1", name).toString());

            employeeMapper.insert(e);
        }

    }

    @Override
    public List<Employee> listByRoleSn(String[] strings) {

        return employeeMapper.listByRoleSn(strings);
    }

    @Override
    public List<Employee> listBySellerId(CustomerQueryObject qo) {
        return employeeMapper.listBySellerId(qo);
    }
}
